package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciReinsuranceRiskMapper;
import com.zatech.cgnci.project.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分入分出险种表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-18
 */
@Service
public class CgnciReinsuranceRiskServiceImpl extends ServiceImpl<CgnciReinsuranceRiskMapper, CgnciReinsuranceRiskDO> implements CgnciReinsuranceRiskService {

    @Resource
    CgnciReinsuranceInfoService reinsuranceInfoService;
    @Resource
    CgnciReinsuranceRiskInsuredService reinsuranceRiskInsuredService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciUnderwritingPolicyService underwritingPolicyService;
    @Resource
    CgnciReinsuranceRiskLayerService reinsuranceRiskLayerService;
    @Resource
    CgnciBusinessStatService businessStatService;

    @Override
    public void deleteReinsuranceRiskForBusiness(Long reinsuranceRiskId, String type) {
        // 查询险种信息
        CgnciReinsuranceRiskDO reinsuranceRiskDO = this.getById(reinsuranceRiskId);
        if (VerifyUtils.isEmpty(reinsuranceRiskDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "reinsuranceRiskId有误");
        }
        if (VerifyUtils.isEmpty(reinsuranceRiskDO.getRiskSubTypeCode())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, reinsuranceRiskId + ":险种riskSubTypeCode为空");
        }

        switch (type) {
            case "1":
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO1 = reinsuranceInfoService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO1)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除分人险种
                this.deleteReinsuranceRisk("1", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                // 删除超净自留险种
                underwritingPolicyService.deleteUnderwritingPolicyRisk(reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                // 删除临分分出险种
                this.deleteReinsuranceRisk("3", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                // 删除合约分出险种
                this.deleteHyReinsuranceRisk("2", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                // 删除统计字段
                businessStatService.deleteBusinessStat(reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());
                break;
            case "2":
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO2 = reinsuranceInfoService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO2)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除临分分出险种
                this.deleteReinsuranceRisk("3", reinsuranceInfoDO2.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());
                break;
            case "3":
                // 查询分人/分出信息
                CgnciReinsurerDO reinsurerDO = reinsurerService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsurerDO)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人方案/再保人");
                }
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO3 = reinsuranceInfoService.getById(reinsurerDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO3)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除分人方案/再保人险种
                this.deleteReinsurerRisk(reinsuranceInfoDO3, reinsuranceRiskDO.getRiskSubTypeCode(), reinsurerDO.getReinsurerId());
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "type参数错误!");
        }
    }

    @Override
    public void deleteReinsuranceRisk(String reinsuranceType, Long businessId, String riskSubTypeCode) {
        CgnciReinsuranceInfoDO reinsuranceInfoDO = reinsuranceInfoService.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, businessId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, reinsuranceType));
        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO)) {
            // 查询分入/分出--险种信息
            CgnciReinsuranceRiskDO reinsuranceRiskDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsuranceInfoDO.getReinsuranceId())
                    .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
            if (VerifyUtils.isNotEmpty(reinsuranceRiskDO)) {
                // 查询分入/分出--险种--标的
                List<CgnciReinsuranceRiskInsuredDO> riskInsuredDOS = reinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskDO.getReinsuranceRiskId()));
                if (VerifyUtils.isNotEmpty(riskInsuredDOS)) {
                    // 处理分入/分出信息汇总字段
                    reinsuranceInfoService.updateReinsuranceColumn(riskInsuredDOS, reinsuranceInfoDO, null);
                    // 删除分入/分出--险种
                    this.removeById(reinsuranceRiskDO.getReinsuranceRiskId());
                }
            }

            // 删除分人方案/再保人险种
            this.deleteReinsurerRisk(reinsuranceInfoDO, riskSubTypeCode, null);
        }
    }

    @Override
    public void deleteHyReinsuranceRisk(String reinsuranceType, Long businessId, String riskSubTypeCode) {
        // 根据业务id和险种code查询合约分出信息
        Integer count = reinsuranceInfoService.countHyReinsuranceInfo(businessId, "2", riskSubTypeCode);
        // 如果删除的险种再合约分出信息的话,删除整个合约分出
        if (count > 0) {
            // 查询合约分出信息
            List<CgnciReinsuranceInfoDO> list = reinsuranceInfoService.list(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                    .eq(CgnciReinsuranceInfoDO::getBusinessId, businessId)
                    .eq(CgnciReinsuranceInfoDO::getReinsuranceType, "2"));
            if (VerifyUtils.isNotEmpty(list)) {
                reinsuranceInfoService.removeByIds(list.stream().map(CgnciReinsuranceInfoDO::getReinsuranceId).collect(Collectors.toSet()));
            }
        }
    }

    @Override
    public void deleteReinsurerRisk(CgnciReinsuranceInfoDO reinsuranceInfoDO, String riskSubTypeCode, Long reinsurerId) {
        // 查询分入方案/再保人
        List<CgnciReinsurerDO> reinsurerList = reinsurerService.list(new LambdaQueryWrapper<CgnciReinsurerDO>()
                .in(CgnciReinsurerDO::getReinsuranceId, reinsuranceInfoDO.getReinsuranceId())
                .eq(VerifyUtils.isNotEmpty(reinsurerId), CgnciReinsurerDO::getReinsurerId, reinsurerId));
        if (VerifyUtils.isNotEmpty(reinsurerList)) {
            reinsurerList.forEach(reinsurer -> {
                // 查询分入/分出--分入方案--险种信息
                CgnciReinsuranceRiskDO reinsurerRiskDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                        .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsurer.getReinsurerId())
                        .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
                if (VerifyUtils.isNotEmpty(reinsurerRiskDO)) {
                    // 查询分入/分出--分入方案--险种信息--标的
                    List<CgnciReinsuranceRiskInsuredDO> reinsurerRiskInsuredDOS = reinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                            .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsurerRiskDO.getReinsuranceRiskId()));
                    if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredDOS)) {
                        // 处理分入/分出--分入方案汇总字段
                        reinsurerService.updateReinsurerColumn(reinsurerRiskInsuredDOS, reinsurer, null);
                        // 删除分入/分出--分入方案--险种
                        this.removeById(reinsurerRiskDO.getReinsuranceRiskId());
                    }
                }
            });
        }
    }

    @Override
    public void deleteReinsuranceRiskForDivideOut(Long reinsuranceRiskId) {
        // 查询险种信息
        CgnciReinsuranceRiskDO reinsuranceRiskDO = this.getById(reinsuranceRiskId);
        if (VerifyUtils.isEmpty(reinsuranceRiskDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "reinsuranceRiskId有误");
        }
        // 删除合约分出分项险种
        this.removeById(reinsuranceRiskId);
    }

}
